package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.UserShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "购物车")
@RestController
@RequestMapping("/user/shoppingCart")
public class UserShopCartController {

    @Autowired
    private UserShopCartService userShopCartService;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result<String> addCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车：{}",shoppingCartDTO);
        userShopCartService.save(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查看购物车
     * @return
     */
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> listResult(){
        log.info("查看购物车");
        List<ShoppingCart> list = userShopCartService.list();
        return Result.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result<String> clean(){
        log.info("清空购物车");
        userShopCartService.delete();
        return Result.success();
    }

    /**
     * 修改购物车
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation("修改购物车")
    @PostMapping("/sub")
    public Result<String> sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("修改购物车：{}",shoppingCartDTO);
        userShopCartService.sub(shoppingCartDTO);
        return Result.success();
    }

}
